package com.sl.utils;

public class NumberFormatToChinese {

	static String[] units = { "", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿", "万亿" };
	static char[] numArray = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };

	/**
	 * @param args
	 */
	/*public static void main(String[] args) {
		int num = 20;
		String numStr = toChinese(num);
		System.out.println("num= " + num + ", convert result: " + numStr);

	}*/

	public static String toChinese(int num) {
		char[] val = String.valueOf(num).toCharArray();
		int len = val.length;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			String m = val[i] + "";
			int n = Integer.valueOf(m);
			boolean isZero = n == 0;
			String unit = units[(len - 1) - i];
			if (isZero) {
				if ('0' == val[i - 1]) {

					continue;
				} else {

					sb.append(numArray[n]);
				}
			} else {
				sb.append(numArray[n]);
				sb.append(unit);
			}
		}
		String s = sb.toString();
		if (s.endsWith("零")) {
			s = s.substring(0, s.length() - 1);
		}
		return s;
	}

}